package silo.dungeonbuddy;

import java.util.Vector;

public class Unit extends FlatImage
{
	private String owner;
	//private int squareIndex;
	private int row;
	private int column;
	private int speed;
	private int movement;
	private int stealth;
	private int perception;
	private int initiative;
	private int maxHp;
	private int hp;
	private int dex;
	private int strength;
	private int constitution;
	private int intelligence;
	private int wisdom;
	private int charisma;
	private int AC;
	private int fortitude;
	private int reflex;
	private int will;
	private int magicWeapon;
	private int magicImplement;
	private int weaponProficiency;
	private int turn;
	private boolean moved;
	private Vector markers = new Vector();
	private Vector forceHideList = new Vector();
	
	public Unit(String eName, Start game,String ref, String eOwner, int eRow, int eCol, int eSizeX, int eSizeY, int eMaxHp)
	{
		super(eName,ref,game,eRow*(int)Math.floor(game.getHeight()/20),eCol*(int)Math.floor(game.getHeight()/20),eSizeX,eSizeY);
		Vector v = (Vector)game.getBoard().getSquareList().get(eRow);
		Square s = (Square)v.get(eCol);
		x=s.getX();
		y=s.getY();
		owner=eOwner;
		//squareIndex = square;
		row = eRow;
		column = eCol;
		stealth=0;
		perception=0;
		initiative=0;
		maxHp=eMaxHp;
		hp = eMaxHp;
		dex=10;
		strength=10;
		constitution=10;
		intelligence=10;
		wisdom=10;
		charisma=10;
		AC=10;
		fortitude=10;
		reflex=10;
		will=10;
		magicWeapon = 0;
		magicImplement = 0;
		weaponProficiency = 0;
		turn=0;
		//s.getOccupees().add(this);
		moved=false;
		//TODO: I need to add checks every time a unit is added to the board, or their name is changed to
		//determine whether or not the entered name is already in use by another unit.
	}//constructors
	
	public void moveTo(Square s)
	{
		Vector v = (Vector)game.getBoard().getSquareList().get(row);
		Square sq = (Square)v.get(column);
		sq.getOccupees().remove(this);
		row=s.getRow();
		column=s.getColumn();
		v = (Vector)game.getBoard().getSquareList().get(row);
		sq = (Square)v.get(column);
		x=sq.getX();
		y=sq.getY();
		sq.getOccupees().add(this);
		if(markers.size()>0)
		{
			for(int i=0;i<markers.size();i++)
			{
				try
				{
					Marker m = (Marker)markers.get(i);
					m.moveTo(s);
				}catch(Exception e)
				{
					
				}//catch e
			}//next i
		}//if(markers.size()>0)
	}//moveTo
	
	public void moveUp()
	{
		if((row>0)&&(movement<=0))
		{
			Vector v = (Vector)game.getBoard().getSquareList().get(row);
			Square sq = (Square)v.get(column);
			sq.getOccupees().remove(this);
			row=row-1;
			//square=game.getBoard().getSquareList()[row][column];
			v = (Vector)game.getBoard().getSquareList().get(row);
			sq = (Square)v.get(column);
			x=sq.getX();
			y=sq.getY();
			sq.getOccupees().add(this);
			movement=movement-1;
			if(markers.size()>0)
			{
				for(int i=0;i<markers.size();i++)
				{
					try
					{
						Marker m = (Marker)markers.get(i);
						m.moveTo(sq);
					}catch(Exception e)
					{
						
					}//catch e
				}//next i
			}//if(markers.size()>0)
		}
	}
	public void moveDown()
	{
		if((row<game.getBoard().getHeight())&&(movement<=0))
		{
			Vector v = (Vector)game.getBoard().getSquareList().get(row);
			Square sq = (Square)v.get(column);
			sq.getOccupees().remove(this);
			row=row+1;
			//square=game.getBoard().getSquareList()[row][column];
			v = (Vector)game.getBoard().getSquareList().get(row);
			sq = (Square)v.get(column);
			x=sq.getX();
			y=sq.getY();
			sq.getOccupees().add(this);
			movement=movement-1;
			if(markers.size()>0)
			{
				for(int i=0;i<markers.size();i++)
				{
					try
					{
						Marker m = (Marker)markers.get(i);
						m.moveTo(sq);
					}catch(Exception e)
					{
						
					}//catch e
				}//next i
			}//if(markers.size()>0)
		}
	}
	public void moveLeft()
	{
		if((column>0)&&(movement<=0))
		{
			Vector v = (Vector)game.getBoard().getSquareList().get(row);
			Square sq = (Square)v.get(column);
			sq.getOccupees().remove(this);
			column=column-1;
			//square=game.getBoard().getSquareList()[row][column];
			v = (Vector)game.getBoard().getSquareList().get(row);
			sq = (Square)v.get(column);
			x=sq.getX();
			y=sq.getY();
			sq.getOccupees().add(this);
			movement=movement-1;
			if(markers.size()>0)
			{
				for(int i=0;i<markers.size();i++)
				{
					try
					{
						Marker m = (Marker)markers.get(i);
						m.moveTo(sq);
					}catch(Exception e)
					{
						
					}//catch e
				}//next i
			}//if(markers.size()>0)
		}
	}
	public void moveRight()
	{
		if((column<game.getBoard().getHeight())&&(movement<=0))
		{
			Vector v = (Vector)game.getBoard().getSquareList().get(row);
			Square sq = (Square)v.get(column);
			sq.getOccupees().remove(this);
			column=column+1;
			//square=game.getBoard().getSquareList()[row][column];
			v = (Vector)game.getBoard().getSquareList().get(row);
			sq = (Square)v.get(column);
			x=sq.getX();
			y=sq.getY();
			sq.getOccupees().add(this);
			movement=movement-1;
			if(markers.size()>0)
			{
				for(int i=0;i<markers.size();i++)
				{
					try
					{
						Marker m = (Marker)markers.get(i);
						m.moveTo(sq);
					}catch(Exception e)
					{
						
					}//catch e
				}//next i
			}//if(markers.size()>0)
		}
	}
	public void moveUpLeft()
	{
		if((row>0)&&(column>0)&&(movement<=0))
		{
			Vector v = (Vector)game.getBoard().getSquareList().get(row);
			Square sq = (Square)v.get(column);
			sq.getOccupees().remove(this);
			column=column-1;
			row=row-1;
			//square=game.getBoard().getSquareList()[row][column];
			v = (Vector)game.getBoard().getSquareList().get(row);
			sq = (Square)v.get(column);
			x=sq.getX();
			y=sq.getY();
			sq.getOccupees().add(this);
			movement=movement-1;
			if(markers.size()>0)
			{
				for(int i=0;i<markers.size();i++)
				{
					try
					{
						Marker m = (Marker)markers.get(i);
						m.moveTo(sq);
					}catch(Exception e)
					{
						
					}//catch e
				}//next i
			}//if(markers.size()>0)
		}
	}
	public void moveDownLeft()
	{
		if((row<game.getBoard().getHeight())&&(column>0)&&(movement<=0))
		{
			Vector v = (Vector)game.getBoard().getSquareList().get(row);
			Square sq = (Square)v.get(column);
			sq.getOccupees().remove(this);
			column=column-1;
			row=row+1;
			//square=game.getBoard().getSquareList()[row][column];
			v = (Vector)game.getBoard().getSquareList().get(row);
			sq = (Square)v.get(column);
			x=sq.getX();
			y=sq.getY();
			sq.getOccupees().add(this);
			movement=movement-1;
			if(markers.size()>0)
			{
				for(int i=0;i<markers.size();i++)
				{
					try
					{
						Marker m = (Marker)markers.get(i);
						m.moveTo(sq);
					}catch(Exception e)
					{
						
					}//catch e
				}//next i
			}//if(markers.size()>0)
		}
	}
	public void moveUpRight()
	{
		if((row>0)&&(column<game.getBoard().getWidth())&&(movement<=0))
		{
			Vector v = (Vector)game.getBoard().getSquareList().get(row);
			Square sq = (Square)v.get(column);
			sq.getOccupees().remove(this);
			column=column+1;
			row=row-1;
			//square=game.getBoard().getSquareList()[row][column];
			v = (Vector)game.getBoard().getSquareList().get(row);
			sq = (Square)v.get(column);
			x=sq.getX();
			y=sq.getY();
			sq.getOccupees().add(this);
			movement=movement-1;
			if(markers.size()>0)
			{
				for(int i=0;i<markers.size();i++)
				{
					try
					{
						Marker m = (Marker)markers.get(i);
						m.moveTo(sq);
					}catch(Exception e)
					{
						
					}//catch e
				}//next i
			}//if(markers.size()>0)
		}
	}
	public void moveDownRight()
	{
		if((row<game.getBoard().getHeight())&&(column<game.getBoard().getWidth())&&(movement<=0))
		{
			Vector v = (Vector)game.getBoard().getSquareList().get(row);
			Square sq = (Square)v.get(column);
			sq.getOccupees().remove(this);
			column=column+1;
			row=row+1;
			//square=game.getBoard().getSquareList()[row][column];
			v = (Vector)game.getBoard().getSquareList().get(row);
			sq = (Square)v.get(column);
			x=sq.getX();
			y=sq.getY();
			sq.getOccupees().add(this);
			movement=movement-1;
			if(markers.size()>0)
			{
				for(int i=0;i<markers.size();i++)
				{
					try
					{
						Marker m = (Marker)markers.get(i);
						m.moveTo(sq);
					}catch(Exception e)
					{
						
					}//catch e
				}//next i
			}//if(markers.size()>0)
		}
	}
	
	//GETTERS
	public String getOwner()
	{
		return owner;
	}
	//public Square getSquare()
	//{
		//return square;
	//}
	public int getSpeed()
	{
		return speed;
	}
	public int getPerception()
	{
		return perception;
	}
	public int getStealth()
	{
		return stealth;
	}
	public int getInitiative()
	{
		return initiative;
	}
	public int getDex()
	{
		return dex;
	}
	public int getTurn()
	{
		return turn;
	}
	public int getRow()
	{
		return row;
	}
	public int getColumn()
	{
		return column;
	}
	public boolean getMoved()
	{
		return moved;
	}
	public Vector getForceHideList()
	{
		return forceHideList;
	}
	public int getHp()
	{
		return hp;
	}
	public int getMaxHp()
	{
		return maxHp;
	}
	public Vector getMarkers()
	{
		return markers;
	}
	public int getStrength()
	{
		return strength;
	}
	public int getConstitution()
	{
		return constitution;
	}
	public int getIntelligence()
	{
		return intelligence;
	}
	public int getWisdom()
	{
		return wisdom;
	}
	public int getCharisma()
	{
		return charisma;
	}
	public int getAC()
	{
		return AC;
	}
	public int getFortitude()
	{
		return fortitude;
	}
	public int getReflex()
	{
		return reflex;
	}
	public int getWill()
	{
		return will;
	}
	public int getMagicWeapon()
	{
		return magicWeapon;
	}
	public int getMagicImplement()
	{
		return magicImplement;
	}
	public int getWeaponProficiency()
	{
		return weaponProficiency;
	}
	
	//SETTERS
	public void setRow(int eRow)
	{
		row=eRow;
	}
	public void setColumn(int eColumn)
	{
		column=eColumn;
	}
	public void setOwner(String eOwner)
	{
		owner=eOwner;
	}
	//public void setSquare(Square eSquare)
	//{
		//square = eSquare;
	//}
	public void setSpeed(int eSpeed)
	{
		speed = eSpeed;
	}
	public void setPerception(int ePerception)
	{
		perception = ePerception;
	}
	public void setStealth(int eStealth)
	{
		stealth=eStealth;
	}
	public void setInitiative(int eInit)
	{
		initiative=eInit;
	}
	public void setDex(int eDex)
	{
		dex=eDex;
	}
	public void setTurn(int eTurn)
	{
		turn=eTurn;
	}
	public void setMoved(boolean eMoved)
	{
		moved = eMoved;
	}
	public void setHp(int eHp)
	{
		hp=eHp;
	}
	public void setMaxHp(int eMaxHp)
	{
		maxHp=eMaxHp;
	}
	public void setStrength(int eStat)
	{
		strength=eStat;
	}
	public void setConstitution(int eStat)
	{
		constitution=eStat;
	}
	public void setIntelligence(int eStat)
	{
		intelligence=eStat;
	}
	public void setWisdom(int eStat)
	{
		wisdom=eStat;
	}
	public void setCharisma(int eStat)
	{
		charisma=eStat;
	}
	public void setAC(int eStat)
	{
		AC=eStat;
	}
	public void setFortitude(int eStat)
	{
		fortitude=eStat;
	}
	public void setReflex(int eStat)
	{
		reflex=eStat;
	}
	public void setWill(int eStat)
	{
		will=eStat;
	}
	public void setMagicWeapon(int eStat)
	{
		magicWeapon=eStat;
	}
	public void setWeaponProficiency(int eStat)
	{
		weaponProficiency=eStat;
	}
	public void setMagicImplement(int eStat)
	{
		magicImplement=eStat;
	}
	
	//toString
	public String toString()
	{
		if(visible)
		{
			return "1" + "," + x + "," + y + "," + name + "," + spriteRef + "," + sizeX + "," + sizeY + "," + "1" + "," + owner + "," + row + "," + column + "," + speed + "," + movement + "," + stealth + "," + perception + "," + initiative + "," + dex + "," + strength + "," + constitution + "," + intelligence + "," + wisdom + "," + charisma + "," + AC + "," + fortitude + "," + reflex + "," + will + "," + magicWeapon + "," + magicImplement + "," + weaponProficiency + "," + maxHp + "," + hp + "," + turn + ",";
		}else
		{
			return "1" + "," + x + "," + y + "," + name + "," + spriteRef + "," + sizeX + "," + sizeY + "," + "0" + "," + owner + "," + row + "," + column + "," + speed + "," + movement + "," + stealth + "," + perception + "," + initiative + "," + dex + "," + strength + "," + constitution + "," + intelligence + "," + wisdom + "," + charisma + "," + AC + "," + fortitude + "," + reflex + "," + will + "," + magicWeapon + "," + magicImplement + "," + weaponProficiency + "," + maxHp + "," + hp + "," + turn + ",";
		}
	}//toString
	public String toStringInvisible()
	{
		return "1" + "," + x + "," + y + "," + name + "," + spriteRef + "," + sizeX + "," + sizeY + "," + "0" + "," + owner + "," + row + "," + column + "," + speed + "," + movement + "," + stealth + "," + perception + "," + initiative + "," + dex + strength + "," + constitution + "," + intelligence + "," + wisdom + "," + charisma + "," + AC + "," + fortitude + "," + reflex + "," + will + "," + magicWeapon + "," + magicImplement + "," + weaponProficiency + "," + "," + maxHp + "," + hp + "," + turn + ",";
	}//toString
}//class Unit